/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package cn.com.oceansoft.modules.modindex.controller;


import cn.com.oceansoft.application.Constant;
import cn.com.oceansoft.application.constant.ConfigInfo;
import cn.com.oceansoft.application.common.util.HttpClientUtil;
import cn.com.oceansoft.framework.kits.ApiResult;
import cn.com.oceansoft.modules.cache.service.BaseCacheService;
import cn.com.oceansoft.modules.modindex.service.IModuleService;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @author cms
 * @version 2016-09-12
 */
@Controller
@RequestMapping(value={"index", ""})
@Api(value = "首页展现",description = "首先展现后台")
public class ModuleController {

    @Resource
    protected ConfigInfo configInfo;
    @Resource
    private IModuleService moduleService;
    @Resource
    private BaseCacheService cacheService;


    /**
     * 便民服务首页
     *
     * @param request
     * @return
     */
    @RequestMapping(method = RequestMethod.GET)
    public String gotomoduleIndex(HttpServletRequest request, Model model) {

        try {
            //加载综合要闻
            String zhywResult=cacheService.get("index:zhywList");
            if(StringUtils.isEmpty(zhywResult)){
                zhywResult = HttpClientUtil.doGet(configInfo.getMsHostUrl() + "/news/page",
                        "ps", 7, "pi", 1, "kw","zhyw","sb","public_time","sd","desc","q_deleted_s_eq","N");
                cacheService.set("index:zhywList",zhywResult,120);
            }
            ApiResult zhywList = JSONObject.parseObject(zhywResult, ApiResult.class);
            model.addAttribute("zhywList",zhywList);

            //加载通知公告
            String tzggResult=cacheService.get("index:tzggResult");
            if(StringUtils.isEmpty(tzggResult)) {
                 tzggResult = HttpClientUtil.doGet(configInfo.getMsHostUrl() + "/news/page",
                        "ps", 6, "pi", 1, "kw", "tzgg","sb","public_time","sd","desc","q_deleted_s_eq","N");
                cacheService.set("index:tzggResult",tzggResult,600);
            }
            ApiResult tzggList = JSONObject.parseObject(tzggResult, ApiResult.class);
            model.addAttribute("tzggList",tzggList);

            //加载警方提示
            String jftsResult=cacheService.get("index:jftsResult");
            if(StringUtils.isEmpty(jftsResult)) {
                 jftsResult = HttpClientUtil.doGet(configInfo.getMsHostUrl() + "/news/page",
                        "ps", 6, "pi", 1, "kw", "jfts","sb","public_time","sd","desc","q_deleted_s_eq","N");
                cacheService.set("index:jftsResult",jftsResult,600);
            }
            ApiResult jftsList = JSONObject.parseObject(jftsResult, ApiResult.class);
            model.addAttribute("jftsList",jftsList);

            //加载政务公开
            String zwgkResult=cacheService.get("index:zwgkResult");
            if(StringUtils.isEmpty(zwgkResult)) {
                 zwgkResult = HttpClientUtil.doGet(configInfo.getMsHostUrl() + "/news/page",
                        "ps", 6, "pi", 1, "kw", "zwgk","sb","public_time","sd","desc","q_deleted_s_eq","N");
                cacheService.set("index:zwgkResult",zwgkResult,600);
            }
            ApiResult zwgkList = JSONObject.parseObject(zwgkResult, ApiResult.class);
            model.addAttribute("zwgkList",zwgkList);

            //加载警务资讯
            String jwzxResult=cacheService.get("index:jwzxResult");
            if(StringUtils.isEmpty(jwzxResult)) {
                jwzxResult = HttpClientUtil.doGet(configInfo.getMsHostUrl() + "/news/page",
                        "ps", 6, "pi", 1, "kw", "jwzx","sb","public_time","sd","desc","q_deleted_s_eq","N");
                cacheService.set("index:jwzxResult",jwzxResult,600);
            }
            ApiResult jwzxList = JSONObject.parseObject(jwzxResult, ApiResult.class);
            model.addAttribute("jwzxList",jwzxList);

            //加载最新公告
            String zxggResult=cacheService.get("index:zxggResult");
            if(StringUtils.isEmpty(zxggResult)) {
                 zxggResult = HttpClientUtil.doGet(configInfo.getMsHostUrl() + "/news/gsgg",
                        "ps", 7, "pi", 1,"sb","public_time","sd","desc","q_deleted_s_eq","N");
                cacheService.set("index:zxggResult",zxggResult,600);
            }
            ApiResult zxggList = JSONObject.parseObject(zxggResult, ApiResult.class);
            model.addAttribute("zxggList",zxggList);

            //加载图片新闻，警务资讯
            String picResult=cacheService.get("index:picResult");
            if(StringUtils.isEmpty(picResult)) {
                picResult = HttpClientUtil.doGet(configInfo.getMsHostUrl() + "/news/page",
                        "ps", 5, "pi", 1,"sb","public_time","sd","desc","kw","jwzx","q_deleted_s_eq","N","q_recommend_s_eq","Y","q_title_pic_s_nn","");
                cacheService.set("index:picResult",picResult,600);
            }
            ApiResult picList = JSONObject.parseObject(picResult, ApiResult.class);
            model.addAttribute("picList",picList);

            //加载办件公示
            String tasks=cacheService.get("index:tasks");
            if(StringUtils.isEmpty(tasks)) {
                tasks = HttpClientUtil.doGet(configInfo.getMsHostUrl() + "tasks/page", "ps", 10, "pi", 1, "sb", "creatordate", "sd", "desc", "q_deleted_s_eq", 'N');
                cacheService.set("index:tasks",tasks,600);
            }
            ApiResult tasksResult = JSONObject.parseObject(tasks, ApiResult.class);
            model.addAttribute("tasks",tasksResult.getData());
            //获取在线人数，总访问量，办件数，注册人数
            int onlineCount=Integer.parseInt(cacheService.get(Constant.ONLINE_NUM));
            int totalCount=Integer.parseInt(cacheService.get(Constant.TOTAL_VISITOR_NUM));
            String taskCountResult=cacheService.get("index:taskCountResult");
            if(StringUtils.isEmpty(taskCountResult)) {
                taskCountResult = HttpClientUtil.doGet(configInfo.getMsHostUrl() + "tasks/count");
                cacheService.set("index:taskCountResult",taskCountResult,600);
            }
            ApiResult  taskCountApiResult= JSONObject.parseObject(taskCountResult, ApiResult.class);
            int taskCount=Integer.parseInt(taskCountApiResult.getData().toString());
            String membersCountResult=cacheService.get("index:membersCountResult");
            if(StringUtils.isEmpty(membersCountResult)) {
                membersCountResult = HttpClientUtil.doGet(configInfo.getMsHostUrl() + "members/count", "q_isdeleted_s_eq", 0);
                cacheService.set("index:membersCountResult",membersCountResult,600);
            }
            ApiResult memberCountApiResult = JSONObject.parseObject(membersCountResult, ApiResult.class);
            int memberCount=Integer.parseInt(memberCountApiResult.getData().toString());
            model.addAttribute("onlineCount",onlineCount);
            model.addAttribute("totalCount",totalCount);
            model.addAttribute("taskCount",taskCount);
            model.addAttribute("memberCount",memberCount);
        }catch (Exception e){
            e.printStackTrace();
        }


        return "module_index/index";
    }



}